from common.mysql.MySQLhelper import get_sql_helper
from common.mysql.entity.shequn_accept_message_info import SheQunAcceptMessageInfo

sqlhelper = get_sql_helper()

def save_accept_message_info(accept_message_info_id, cmsg, today, user_id, type, mul_ori, user_name):
    if user_id == cmsg.from_user_id:
        msg_friend_id = cmsg.to_user_id
        msg_friend_name = cmsg.to_user_nickname
    else:
        msg_friend_id = cmsg.from_user_id
        msg_friend_name = cmsg.from_user_nickname
    sqlhelper.insert_one(
        "INSERT INTO shequn_accept_message_info (id, ctype_name, ctype_value, msg_actual_user_id, msg_actual_user_nickname,"
        "msg_content, create_time, update_time, msg_from_user_id, msg_from_user_nickname, msg_is_at, msg_is_group, msg_id, my_msg, msg_other_user_id,"
        "msg_other_user_nickname, msg_to_user_id, msg_to_user_nickname, user_id, msg_friend_id, msg_friend_name, mul_value, user_name) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ",
        (accept_message_info_id, type.name, type.value, cmsg.actual_user_id, cmsg.actual_user_nickname, cmsg.content,
         today, today, cmsg.from_user_id, cmsg.from_user_nickname, cmsg.is_at, cmsg.is_group, cmsg.msg_id, cmsg.my_msg,
         cmsg.other_user_id, cmsg.other_user_nickname, cmsg.to_user_id, cmsg.to_user_nickname, user_id, msg_friend_id,
         msg_friend_name, mul_ori, user_name))

def get_accept_user_info(user_id):
    results = sqlhelper.fetch_all("select * from shequn_accept_message_info where user_id = %s; ", (user_id))

    # Create a list to store instances of ShequnAcceptMessageInfo
    shequn_accept_message_info_list = []

    # Iterate through the results and create instances of ShequnAcceptMessageInfo
    for row in results:
        shequn_accept_message_info = SheQunAcceptMessageInfo(*row)
        shequn_accept_message_info_list.append(shequn_accept_message_info)
    return shequn_accept_message_info_list


def update_accept_user_list(update_accept_data):
    sql = 'UPDATE shequn_accept_message_info SET msg_from_user_id = %s, msg_to_user_id = %s, user_id = %s, msg_friend_id = %s WHERE id = %s; '
    sqlhelper.update_list(sql, update_accept_data)

def delete_accept_message_info_by_user_id(user_id):
    sqlhelper.delete("DELETE from shequn_accept_message_info where user_id = %s", (user_id))